package com.jmpt.fangbohui.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.LoginBean;
import com.jmpt.fangbohui.dao.LoginSQLiteOperator;
import com.jmpt.fangbohui.pic.PublishedActivity;
import com.jmpt.fangbohui.ui.base.BaseHeadFragment;
import com.jmpt.fangbohui.ui.personalcenter.AddBBClientActivity;
import com.jmpt.fangbohui.ui.personalcenter.AlreadyBBClientActivity;
import com.jmpt.fangbohui.ui.personalcenter.BrokerInfoDetailActivity;
import com.jmpt.fangbohui.ui.personalcenter.BrokerMyIndentActivity;
import com.jmpt.fangbohui.ui.personalcenter.BrokerNewHouseFloor;
import com.jmpt.fangbohui.ui.personalcenter.BrokerOrderActivity;
import com.jmpt.fangbohui.ui.personalcenter.EnterHouseActivity;
import com.jmpt.fangbohui.ui.personalcenter.LoginActivity;
import com.jmpt.fangbohui.ui.personalcenter.ManageHouseActivity;
import com.jmpt.fangbohui.ui.personalcenter.PersonalOutstandingActivity;
import com.jmpt.fangbohui.ui.personalcenter.SeeHouseClientActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserAleradyBuyRentTwoHandHouseActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserAlreadyRentSellIndentActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserCollectNewhouseActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserCollectTwoHandHouseActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserIWantRentSellHouseActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserInfoDetailActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserIssueRentSellNeedActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserManageRentSellHouseActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserManageRentSellNeedActivity;
import com.jmpt.fangbohui.ui.personalcenter.UserMyNewhouseIndentActivity;
import com.jmpt.fangbohui.utils.DebugLog;

public class MainUserFragment extends BaseHeadFragment implements OnClickListener {

	private View rootView;
	private Context mContext;
	private LinearLayout ll_fragment_user;
	private LinearLayout ll_fragment_broker;
	private RelativeLayout rl_user_info_top;
	private ImageView iv_user_info_show_icon;
	private TextView tv_user_info_show_nicheng;
	private TextView tv_user_info_show_phone;

	public MainUserFragment() {
		// Required empty public constructor
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		DebugLog.i("MainUserFragment  --->  onCreateView");
		mContext = getActivity();
		//确保不会重复添加
		if (rootView != null) {
			ViewGroup parent = (ViewGroup) rootView.getParent();
			if (parent != null) {
				parent.removeView(rootView);
			}
			//返回原有的布局对象  不往下走了
			return rootView;
		}
		super.onCreateView(inflater, container, savedInstanceState);
		// 这里开始初始化布局中的控件
		// assignViews(rootView);
		showTitle("个人中心");
		initView();
		judgeView();
		rootView = setContentView(rootView);
		rl_user_info_top.setOnClickListener(this);
		
        
        
        
        
		return rootView;
	}



	//判断加载哪个界面  未登录用户、普通用户、经纪人
	private void judgeView(){
		LoginSQLiteOperator oper = new LoginSQLiteOperator(getActivity());
		LoginBean loginBean = oper.query();
		if(loginBean == null){
			//iv_user_info_show_icon.setImageDrawable(drawable);
			tv_user_info_show_nicheng.setText("点击登录");
			tv_user_info_show_phone.setText("");
			//未登录状态，显示用户，隐藏经纪人
			ll_fragment_user.setVisibility(View.VISIBLE);
			ll_fragment_broker.setVisibility(View.GONE);
			
		}else{
			
			if(loginBean.getRetval().getType()==1){
				//普通用户
				ll_fragment_user.setVisibility(View.VISIBLE);
				ll_fragment_broker.setVisibility(View.GONE);
				initUserEvent();
			}else{
				//经纪人
				ll_fragment_user.setVisibility(View.GONE);
				ll_fragment_broker.setVisibility(View.VISIBLE);
				initBrokerEvent();
			}
			//iv_user_info_show_icon.setImageDrawable(drawable);
			tv_user_info_show_nicheng.setText(loginBean.getRetval().getType()==1 ? loginBean.getRetval().getUserName():loginBean.getRetval().getAgentName());
		}
		
	}
	private void initBrokerEvent() {
		
		RelativeLayout rl_my_pager_broker_personal_outstanding = (RelativeLayout) rootView.findViewById(R.id.rl_my_pager_broker_personal_outstanding);
		RelativeLayout rl_my_pager_broker_personal_info = (RelativeLayout) rootView.findViewById(R.id.rl_my_pager_broker_personal_info);
		RelativeLayout rl_my_pager_broker_add_client = (RelativeLayout) rootView.findViewById(R.id.rl_my_pager_broker_add_client);
		RelativeLayout rl_my_pager_broker_client_list = (RelativeLayout) rootView.findViewById(R.id.rl_my_pager_broker_client_list);
		RelativeLayout rl_my_pager_broker_entering_house = (RelativeLayout) rootView.findViewById(R.id.rl_my_pager_broker_entering_house);
		RelativeLayout rl_my_pager_broker_managehouse = (RelativeLayout) rootView.findViewById(R.id.rl_my_pager_broker_managehouse);
		RelativeLayout rl_my_pager_broker_my_indent= (RelativeLayout) rootView.findViewById(R.id.rl_my_pager_broker_my_indent);
		RelativeLayout rl_newhouse_floor= (RelativeLayout) rootView.findViewById(R.id.rl_newhouse_floor);
		RelativeLayout rl_see_house_client= (RelativeLayout) rootView.findViewById(R.id.rl_see_house_client);
		rl_newhouse_floor.setOnClickListener(this);
		rl_my_pager_broker_my_indent.setOnClickListener(this);
		rl_my_pager_broker_managehouse.setOnClickListener(this);
		rl_my_pager_broker_entering_house.setOnClickListener(this);
		rl_my_pager_broker_client_list.setOnClickListener(this);
		rl_my_pager_broker_add_client.setOnClickListener(this);
		rl_my_pager_broker_personal_outstanding.setOnClickListener(this);
		rl_my_pager_broker_personal_info.setOnClickListener(this);
		rl_see_house_client.setOnClickListener(this);
		rootView.findViewById(R.id.rl_my_pager_broker_orders).setOnClickListener(this);
	}


	private void initUserEvent() {
		RelativeLayout rl_fragment_user_my_newhouse_indent = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_my_newhouse_indent);
		RelativeLayout rl_fragment_user_already_buy_rent_two_hand_house = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_already_buy_rent_two_hand_house);
		RelativeLayout rl_fragment_user_already_rent_sell_indent = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_already_rent_sell_indent);
		RelativeLayout rl_fragment_user_i_want_rent_sell_house = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_i_want_rent_sell_house);
		RelativeLayout rl_fragment_user_manage_rent_sell_house = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_manage_rent_sell_house);
		RelativeLayout rl_fragment_user_issue_rent_sell_need = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_issue_rent_sell_need);
		RelativeLayout rl_fragment_user_manage_rent_sell_need = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_manage_rent_sell_need);
		RelativeLayout rl_fragment_user_collect_newhouse = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_collect_newhouse);
		RelativeLayout rl_fragment_user_collect_two_hand_house = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_collect_two_hand_house);
		RelativeLayout rl_fragment_user_personal_info = (RelativeLayout) rootView.findViewById(R.id.rl_fragment_user_personal_info);
		rl_fragment_user_my_newhouse_indent.setOnClickListener(this);
		rl_fragment_user_already_buy_rent_two_hand_house.setOnClickListener(this);
		rl_fragment_user_already_rent_sell_indent.setOnClickListener(this);
		rl_fragment_user_i_want_rent_sell_house.setOnClickListener(this);
		rl_fragment_user_manage_rent_sell_house.setOnClickListener(this);
		rl_fragment_user_issue_rent_sell_need.setOnClickListener(this);
		rl_fragment_user_manage_rent_sell_need.setOnClickListener(this);
		rl_fragment_user_collect_newhouse.setOnClickListener(this);
		rl_fragment_user_collect_two_hand_house.setOnClickListener(this);
		rl_fragment_user_personal_info.setOnClickListener(this);
		
		
	}


	//查找界面控件
	private void assignViews(View rootView) {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 初始化空间
	 */
	private void initView() {
		rootView = View.inflate(getActivity(), R.layout.fragment_user, null);
		ll_fragment_user = (LinearLayout) rootView.findViewById(R.id.ll_fragment_user);
		ll_fragment_broker = (LinearLayout) rootView.findViewById(R.id.ll_fragment_broker);
		rl_user_info_top = (RelativeLayout) rootView.findViewById(R.id.rl_user_info_top);
		iv_user_info_show_icon = (ImageView) rootView.findViewById(R.id.iv_user_info_show_icon);
		tv_user_info_show_nicheng = (TextView) rootView.findViewById(R.id.tv_user_info_show_nicheng);
		tv_user_info_show_phone = (TextView) rootView.findViewById(R.id.tv_user_info_show_phone);
	}

	private void startActivityForType(){

		LoginSQLiteOperator oper = new LoginSQLiteOperator(getActivity());
		LoginBean loginBean = oper.query();
		if(loginBean== null){
			//跳转到登录界面
			Intent loginIntent = new Intent(mContext,LoginActivity.class);
			mContext.startActivity(loginIntent);
		}else{
			Intent edit;
			//跳转到信息修改界面
			if(loginBean.getRetval().getType()==1){
				edit= new Intent(mContext,UserInfoDetailActivity.class);
				mContext.startActivity(edit);
			}else{
				edit= new Intent(mContext,BrokerInfoDetailActivity.class);
				mContext.startActivity(edit);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//顶部个人信息
		case R.id.rl_user_info_top:
			startActivityForType();
		break;
		//新房合作楼盘
		case R.id.rl_newhouse_floor:
			Intent nfIntent = new Intent(mContext,BrokerNewHouseFloor.class);
			mContext.startActivity(nfIntent);
		break;
		//经纪人 我的订单
		case R.id.rl_my_pager_broker_my_indent:
			Intent bmiIntent = new Intent(mContext,BrokerMyIndentActivity.class);
			mContext.startActivity(bmiIntent);
		break;
		
			//个人业绩
		case R.id.rl_my_pager_broker_personal_outstanding:
			Intent poIntent = new Intent(mContext,PersonalOutstandingActivity.class);
			mContext.startActivity(poIntent);
		break;
		//经纪人的个人信息界面
		case R.id.rl_my_pager_broker_personal_info:
			startActivityForType();
		break;
		//经纪人-新添加报备客户
		case R.id.rl_my_pager_broker_add_client:
			Intent AddBBCIntent = new Intent(mContext,AddBBClientActivity.class);
			mContext.startActivity(AddBBCIntent);
		break;
		//经纪人-已报备客户列表
		case R.id.rl_my_pager_broker_client_list:
			Intent ABBCIntent = new Intent(mContext,AlreadyBBClientActivity.class);
			mContext.startActivity(ABBCIntent);
		break;
		//经纪人-房源录入
		case R.id.rl_my_pager_broker_entering_house:
			//TODO
			Intent ehaIntent = new Intent(mContext,EnterHouseActivity.class);
			mContext.startActivity(ehaIntent);
//			Intent intent = new Intent(mContext,PublishedActivity.class);
//			mContext.startActivity(intent);
		break;
		//经纪人-房源管理
		case R.id.rl_my_pager_broker_managehouse:
			Intent mhaIntent = new Intent(mContext,ManageHouseActivity.class);
			mContext.startActivity(mhaIntent);
			break;
		//我的新房订单
		case R.id.rl_fragment_user_my_newhouse_indent:
			Intent umniIntent = new Intent(mContext,UserMyNewhouseIndentActivity.class);
			mContext.startActivity(umniIntent);
			
		break;
		//已购租二手房
		case R.id.rl_fragment_user_already_buy_rent_two_hand_house:
			Intent uabrthhIntent = new Intent(mContext,UserAleradyBuyRentTwoHandHouseActivity.class);
			mContext.startActivity(uabrthhIntent);
		break;
		//已租售订单
		case R.id.rl_fragment_user_already_rent_sell_indent:
			Intent uarsiIntent = new Intent(mContext,UserAlreadyRentSellIndentActivity.class);
			mContext.startActivity(uarsiIntent);
			
		break;
		//我要租售房源
		case R.id.rl_fragment_user_i_want_rent_sell_house:
			Intent uiwrshIntent = new Intent(mContext,UserIWantRentSellHouseActivity.class);
			mContext.startActivity(uiwrshIntent);
		break;
		//管理租售房源
		case R.id.rl_fragment_user_manage_rent_sell_house:
			Intent umrshIntent = new Intent(mContext,UserManageRentSellHouseActivity.class);
			mContext.startActivity(umrshIntent);
		break;
		//发布租售需求
		case R.id.rl_fragment_user_issue_rent_sell_need:
			Intent uirsnIntent = new Intent(mContext,UserIssueRentSellNeedActivity.class);
			mContext.startActivity(uirsnIntent);
		break;
		//管理租售需求
		case R.id.rl_fragment_user_manage_rent_sell_need:
			Intent umrsnIntent = new Intent(mContext,UserManageRentSellNeedActivity.class);
			mContext.startActivity(umrsnIntent);
		break;
		//收藏的新房楼盘
		case R.id.rl_fragment_user_collect_newhouse:
			Intent ucnIntent = new Intent(mContext,UserCollectNewhouseActivity.class);
			mContext.startActivity(ucnIntent);
		break;
		//收藏的二手房源
		case R.id.rl_fragment_user_collect_two_hand_house:
			Intent ucthhIntent = new Intent(mContext,UserCollectTwoHandHouseActivity.class);
			mContext.startActivity(ucthhIntent);
		break;
		//收藏的二手房源
		case R.id.rl_my_pager_broker_orders:
			Intent orderIntent = new Intent(mContext,BrokerOrderActivity.class);
			mContext.startActivity(orderIntent);
			break;
		//个人信息
		case R.id.rl_fragment_user_personal_info:
			startActivityForType();
		break;
		//预约看房客户列表
		case R.id.rl_see_house_client:
			Intent see_house_client = new Intent(mContext,SeeHouseClientActivity.class);
			mContext.startActivity(see_house_client);
			break;
		}
	}
	
	@Override
	public void onResume() {
		judgeView();
		DebugLog.i("onResume");
		super.onResume();
	}


}
